package com.enterpriseDigitalPlatform.controller.renshi.attendanceMonth;

import com.enterpriseDigitalPlatform.dto.JsonRes;
import com.enterpriseDigitalPlatform.dto.PageRes;
import com.enterpriseDigitalPlatform.exception.ServiceException;
import com.enterpriseDigitalPlatform.util.RequiredCheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/renshi-attendanceMonth")
public class AttendanceMonthC {
    private final AttendanceMonthDao attendanceMonthDao;

    @Autowired
    public AttendanceMonthC(AttendanceMonthDao attendanceMonthDao) {
        this.attendanceMonthDao = attendanceMonthDao;
    }

    // 批量导入
    // 获取导入模板 带人员id 部门id

    @PostMapping("/editData")
    public Object editData(@RequestBody AttendanceMonthDTO attendanceMonthDTO) {
        if (RequiredCheckUtil.checkInt(attendanceMonthDTO.getId())) {
            throw new ServiceException("id不能为空");
        }
        int row = attendanceMonthDao.editData(attendanceMonthDTO);
        return new JsonRes<>(row);
    }

    @PostMapping("/getList")
    public Object getList(@RequestBody AttendanceMonthDTO attendanceMonthDTO) {
        if (RequiredCheckUtil.checkInt(attendanceMonthDTO.getPage())) {
            attendanceMonthDTO.setPage(1);
        }
        if (RequiredCheckUtil.checkInt(attendanceMonthDTO.getPageSize())) {
            attendanceMonthDTO.setPageSize(10);
        }
        PageRes<AttendanceMonth> pageRes = attendanceMonthDao.getList(attendanceMonthDTO);
        return new JsonRes<>(pageRes);
    }

    @PostMapping("/getData")
    public Object getData(@RequestBody AttendanceMonthDTO attendanceMonthDTO) {
        if (RequiredCheckUtil.checkInt(attendanceMonthDTO.getId())) {
            throw new ServiceException("id不能为空");
        }
        PageRes<AttendanceMonth> pageRes = attendanceMonthDao.getList(attendanceMonthDTO);
        AttendanceMonth attendanceMonth = new AttendanceMonth();
        List<AttendanceMonth> list = pageRes.getData();
        if (!list.isEmpty()) {
            attendanceMonth = list.get(0);
        }
        return new JsonRes<>(attendanceMonth);
    }
}
